#include "ImageCell.h"
#include <QApplication>
#include <QPicture>
#include <QMouseEvent>


namespace UserManagementSystem {


ImageCell::ImageCell(QWidget *parent, bool has_delete, const QString pix_path)
    : QWidget{parent}
{
    this->setFixedSize(m_itemWidth,m_itemWidth);

    m_mainLayout=new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(1,1,1,1);//将布局内部的边界空隙设置为0
    QString m_addPicPath=QApplication::applicationDirPath()+"/res/BaseWidget/add_image.png";
    QString m_deleteIconPath=QApplication::applicationDirPath()+"/res/BaseWidget/delete_image.png";


    //根据bool标识选择读取图片
    if(!has_delete){
        //没有删除按钮，加载默认图片
        QPixmap pix(m_addPicPath);
        m_picture.setPixmap(pix.scaled(m_itemWidth-2, m_itemWidth-2, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }else{

        //读入图片
        QPixmap pix(pix_path);
        m_picture.setPixmap(pix.scaled(m_itemWidth-2, m_itemWidth-2, Qt::KeepAspectRatio, Qt::SmoothTransformation));

        //新增一个右上角的按钮
        m_deleteBtn=new QPushButton(this);
        m_deleteBtn->setFixedSize(20,20);
        m_deleteBtn->setIcon(QIcon(m_deleteIconPath));
        m_deleteBtn->setGeometry(this->rect().right()-30,10,20,20);//移动到右上角
        m_deleteBtn->setStyleSheet("background-color:rgba(255, 255, 255, 0);");

        connect(m_deleteBtn,&QPushButton::clicked,this,&ImageCell::On_Delete_Image);
    }
    //最后读入图片
    m_mainLayout->addWidget(&m_picture);
    m_picture.lower();//图片放在底层

    //添加图片的信号槽呢

    this->setStyleSheet("border-radius:10px;background-color:#f7f9fb;");



}

void ImageCell::mouseReleaseEvent(QMouseEvent *event)
{
    if(!m_hasDeleteBtn){
        //标识为占位符，触发add信号
        qDebug()<<"触发添加图片信号";
        emit On_Add_Image();

    }else{
        //判断鼠标是否在btn按钮的位置
        QRect btn_rect=m_deleteBtn->rect();
        if(btn_rect.contains(event->pos())){
            qDebug()<<"触发删除信号";
            emit On_Delete_Image();
        }

    }
}

}

